home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / artdisplay / LocalCoverArtSearchGIO.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.5 KB  |  173 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import rhythmdb
  6. import rb
  7. import gobject
  8. import gio
  9. IMAGE_NAMES = [
  10.     'cover',
  11.     'album',
  12.     'albumart',
  13.     '.folder',
  14.     'folder']
  15. ITEMS_PER_NOTIFICATION = 10
  16. ART_SAVE_NAME = 'Cover.jpg'
  17. ART_SAVE_FORMAT = 'jpeg'
  18. ART_SAVE_SETTINGS = {
  19.     'quality': '100' }
  20.  
  21. def file_root(f_name):
  22.     return os.path.splitext(f_name)[0].lower()
  23.  
  24.  
  25. def shared_prefix_length(a, b):
  26.     l = 0
  27.     while a[l] == b[l]:
  28.         l = l + 1
  29.     return l
  30.  
  31.  
  32. class LocalCoverArtSearch:
  33.     
  34.     def __init__(self):
  35.         pass
  36.  
  37.     
  38.     def _enum_dir_cb(self, fileenum, result, .3):
  39.         (results, on_search_completed_cb, entry, args) = .3
  40.         
  41.         try:
  42.             files = fileenum.next_files_finish(result)
  43.             if files is None or len(files) == 0:
  44.                 print 'okay, done; got %d files' % len(results)
  45.                 on_search_completed_cb(self, entry, results, *args)
  46.                 return None
  47.             for f in files:
  48.                 ct = f.get_attribute_string('standard::fast-content-type')
  49.                 if ct.startswith('image/') and f.get_attribute_boolean('access::can-read'):
  50.                     results.append(f.get_name())
  51.                     continue
  52.                 len(files) == 0
  53.             
  54.             fileenum.next_files_async(ITEMS_PER_NOTIFICATION, callback = self._enum_dir_cb, user_data = (results, on_search_completed_cb, entry, args))
  55.         except Exception:
  56.             e = None
  57.             print 'okay, probably done: %s' % e
  58.             on_search_completed_cb(self, entry, results, *args)
  59.  
  60.  
  61.     
  62.     def search(self, db, entry, on_search_completed_cb, *args):
  63.         self.file = gio.File(entry.get_playback_uri())
  64.         if self.file.get_uri_scheme() in ('http', 'cdda'):
  65.             print 'not searching for local art for %s' % self.file.get_uri()
  66.             on_search_completed_cb(self, entry, [], *args)
  67.             return None
  68.         self.artist = db.entry_get(entry, rhythmdb.PROP_ARTIST)
  69.         self.album = db.entry_get(entry, rhythmdb.PROP_ALBUM)
  70.         print 'searching for local art for %s' % self.file.get_uri()
  71.         parent = self.file.get_parent()
  72.         enumfiles = parent.enumerate_children(attributes = 'standard::fast-content-type,access::can-read,standard::name')
  73.         enumfiles.next_files_async(ITEMS_PER_NOTIFICATION, callback = self._enum_dir_cb, user_data = ([], on_search_completed_cb, entry, args))
  74.  
  75.     
  76.     def search_next(self):
  77.         return False
  78.  
  79.     
  80.     def get_best_match_urls(self, results):
  81.         parent = self.file.get_parent()
  82.         for name in [
  83.             file_root(self.file.get_basename())] + IMAGE_NAMES:
  84.             for f_name in results:
  85.                 if file_root(f_name) == name:
  86.                     yield parent.resolve_relative_path(f_name).get_uri()
  87.                     continue
  88.             
  89.         
  90.         artist = self.artist.lower()
  91.         album = self.album.lower()
  92.         for f_name in results:
  93.             f_root = file_root(f_name).lower()
  94.             if f_root.find(artist) != -1 and f_root.find(album) != -1:
  95.                 yield parent.resolve_relative_path(f_name).get_uri()
  96.                 continue
  97.         
  98.         match = (2, None)
  99.         for f_name in results:
  100.             pl = shared_prefix_length(f_name, self.file.get_basename())
  101.             if pl > match[0]:
  102.                 match = (pl, f_name)
  103.                 continue
  104.         
  105.         if match[1] is not None:
  106.             yield parent.resolve_relative_path(match[1]).get_uri()
  107.         
  108.  
  109.     
  110.     def pixbuf_save(self, plexer, pixbuf, uri):
  111.         
  112.         def pixbuf_cb(buf):
  113.             f = gio.File(uri)
  114.             f.replace_contents_async(buf, plexer.send())
  115.             yield None
  116.             (file, result) = (_,)
  117.             
  118.             try:
  119.                 file.replace_contents_finish(result)
  120.             except Exception:
  121.                 plexer.receive()
  122.                 e = plexer.receive()
  123.                 print 'error creating "%s": %s' % (uri, e)
  124.             except:
  125.                 plexer.receive()
  126.  
  127.  
  128.         pixbuf.save_to_callback(pixbuf_cb, ART_SAVE_FORMAT, ART_SAVE_SETTINGS)
  129.  
  130.     
  131.     def _save_dir_cb(self, enum, result, .3):
  132.         (db, entry, dir, pixbuf) = .3
  133.         (artist, album) = [ db.entry_get(entry, x) for x in [
  134.             rhythmdb.PROP_ARTIST,
  135.             rhythmdb.PROP_ALBUM] ]
  136.         
  137.         try:
  138.             files = enum.next_files_finish(result)
  139.             for f in files:
  140.                 ct = f.get_attribute_string('standard::fast-content-type')
  141.                 if ct.startswith('image/') or ct.startswith('x-directory/'):
  142.                     continue
  143.                 
  144.                 uri = dir.resolve_relative_path(f.get_name())
  145.                 u_entry = db.entry_lookup_by_location(uri)
  146.                 print 'Not saving local art; encountered unknown file (%s)' % uri
  147.                 enum.close()
  148.                 return None
  149.         except Exception:
  150.             []
  151.             e = []
  152.             []
  153.             print 'Error reading "%s": %s' % (dir, exception)
  154.         except:
  155.             []
  156.  
  157.  
  158.     
  159.     def save_pixbuf(self, db, entry, pixbuf):
  160.         uri = entry.get_playback_uri()
  161.         if uri is None or uri == '':
  162.             return None
  163.         f = gio.File(uri)
  164.         if uri.get_uri_scheme() == 'http':
  165.             print 'not saving local art for %s' % uri
  166.             return None
  167.         print 'checking whether to save local art for %s' % uri
  168.         parent = f.get_parent()
  169.         enumfiles = parent.enumerate_children(attributes = 'standard::fast-content-type,access::can-read,standard::name')
  170.         enumfiles.next_files_async(ITEMS_PER_NOTIFICATION, callback = self._save_dir_cb, user_data = (db, entry, parent, pixbuf))
  171.  
  172.  
  173.